﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Props.CustomWeb.Models;
using Facebook;
using Props.Backend;

namespace Props.CustomWeb.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var profile = new VendorService().GetProfileByName(base.VendorName);

            if (profile == null)
                return RedirectToAction("PageNotFound");

            HomeIndexModel model = new HomeIndexModel()
            {
                FacebookSession = FacebookSession.Current,
                VendorProfile = profile,
                VendorTitle = profile.Title,
                BackgroundColor = profile.BackgroundColor,
                BackgroundUrl = profile.BackgroundUrl
            };

            return View(model);
        }

        public ActionResult FacebookLogin()
        {
            var accessToken = HttpContext.Request["accessToken"];
            HttpContext.Session["AccessToken"] = accessToken;
            
            return RedirectToAction("Index", "Home");
        }

        public ActionResult FacebookLogout()
        {
            var uri = FacebookSession.LogOut(VendorName);

            if (uri == null)
                return RedirectToAction("Index", "Home");
            else
                return Redirect(uri.AbsoluteUri);
        }

        public ActionResult PageNotFound()
        {
            return View();
        }
    }
}
